package net.roobook.cms.common.utils;

import org.quartz.impl.triggers.CronTriggerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @author cos
 * @email 327878610@qq.com
 * @link https://roobook.net
 * @Description TODO
 * @date 2023/9/25 21:16
 **/
public class CronUtils {
    /**
     * 日志对象
     */
    protected static Logger logger = LoggerFactory.getLogger(CronUtils.class);
    public static boolean isValidExpression(final String cronExpression) {
        CronTriggerImpl trigger = new CronTriggerImpl();
        try {
            trigger.setCronExpression(cronExpression);
            Date date = trigger.computeFirstFireTime(null);
            return date != null && date.after(new Date());
        } catch (Exception e) {
            logger.error("invalid expression:{},error msg:{}", cronExpression, e.getMessage());
        }
        return false;
    }
}
